/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package biz;

import da.DepartmentJpaController;
import entities.Department;
import java.util.List;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.TypedQuery;
import javax.transaction.UserTransaction;

/**
 *
 * @author TungGreen
 */
@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class DeparmentProgess {

    @Resource
    private UserTransaction utx;
    @PersistenceUnit(unitName = "ProjectSem4PU")
    private EntityManagerFactory emf = null;
    private DepartmentJpaController jpa;

    public DepartmentJpaController getJpa() {
        if (jpa == null) {
            jpa = new DepartmentJpaController(utx, emf);
        }
        return jpa;
    }
    /**
     * 
     * Function get all Department 
     * @return list<department>
     * 
     */
    public List<Department> getAllDepartment() {
        return getJpa().findDepartmentEntities();
    }
    /**
     * 
     * Function get department by departmentID
     * @param id
     * @return 
     */
    public Department getDepartment(int id) {
        String query = "SELECT d FROM Department d WHERE d.id = :id";
        TypedQuery<Department> tq = emf.createEntityManager().createQuery(query, Department.class);
        tq.setParameter("id", id);
        try {
            return tq.getSingleResult();
        } catch (Exception e) {
            return null;
        }
    }
}
